﻿Shader "Custom/Box Alpha" {
	Properties
	{
		_Color("Color", Color) = (1,1,1,1)
		_Color2("Color", Color) = (1,1,1,1)
		_MainTex("Texture", 2D) = "white" {}
		_Power("偏移威力",Range(-0.01,0.01)) = -0.001
	}
		SubShader
	{
		Tags{ "RenderType" = "Opaque" }
		Tags{ "Queue" = "Transparent+2" }
		LOD 100
			ZTest Always ZWrite On

		Pass
	{
		Blend SrcAlpha OneMinusSrcAlpha
		CGPROGRAM
#pragma vertex vert 
#pragma fragment frag
		// make fog work
#pragma multi_compile_fog 

		//#include "UnityCG.cginc"
#include "Lighting.cginc"

		struct appdata
	{
		float4 vertex : POSITION;
		float2 uv : TEXCOORD0;
		float4 color : COLOR0;
		float3 normal : NORMAL;
	};

	struct v2f
	{
		float2 uv : TEXCOORD0;
		UNITY_FOG_COORDS(1)
			float4 vertex : SV_POSITION;
		float4 color : COLOR0;
		float3 worldPos : TEXCOORD2;
		float3 normal : NORMAL;
	};

	sampler2D _MainTex;
	float4 _MainTex_ST;
	fixed4 _Color;
	fixed4 _Color2;
	float _Power;

	v2f vert(appdata v)
	{
		v2f o;
		o.vertex = UnityObjectToClipPos(v.vertex + float4(v.normal * _Power, 0.1));
		o.uv = TRANSFORM_TEX(v.uv, _MainTex);
		o.normal = v.normal;
		UNITY_TRANSFER_FOG(o,o.vertex);




		fixed3 a = v.normal;
		fixed4 color;
		if (a.x != 0)
		{
			if (a.x< 0)
			{
				a.x = -a.x;
			}
			color = fixed4(_Color.r, _Color.r, _Color.r, 1);
		}
		else if (a.y != 0)
		{
			if (a.y < 0)
			{
				a.y = -a.y;
			}
			color = fixed4(_Color.g, _Color.g, _Color.g, 1);
		}
		else if (a.z != 0)
		{
			if (a.z < 0)
			{
				a.z = -a.z;
			}
			color = fixed4(_Color.b, _Color.b, _Color.b, 1);
		}

		o.color = v.color * color;
		o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
		return o;
	}

	fixed4 frag(v2f i) : SV_Target
	{
		fixed4 col;


		col = tex2D(_MainTex, i.uv) * i.color  * _Color2

		// sample the texture
		/*fixed4 col = tex2D(_MainTex, i.uv)  * fixed4(i.color, _Color.a)  * _Color;
		col.a = _Color.a;*/
		// apply fog
		UNITY_APPLY_FOG(i.fogCoord, col);
		clip(i.worldPos.y - 0);
		return col;
		}
			ENDCG
		}
	}
		Fallback "Diffuse"
}
